const mysql = require('mysql');
const config = require('./config/config');
const pool = mysql.createPool(config.database);

let query = (sql, values)=>{
  return new Promise((resolve, reject)=>{
    pool.getConnection((err, connection)=>{
      if (err) {
        console.log(`查询数据库失败:${err}`);
        reject(err);
        return;
      }
      connection.query(sql, values, (error, rows)=>{
        if (error) {
          reject(error);
          console.log(`查询数据库失败:${error}`);
          return;
        }
        resolve(rows);
        console.log("查询数据库成功" + rows);
        connection.release();
      });
    });
  });
};

module.exports = {query};
